package cn.pli.bike.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.framemodule.base.BaseDelegate;
import com.framemodule.base.ImageLoaderHelper;
import com.framemodule.utils.CommonUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.pli.bike.R;
import cn.pli.bike.bean.UserBean;
import cn.pli.bike.bean.UserInfoSetBean;

/**
 * Created by shangdongzhou on 2017/11/8.
 */

public class UserInfoDelegate extends BaseDelegate {
    public static final int YOU_WALLET = 0;
    public static final int YOU_CARD = 1;
    public static final int YOU_JOURNEY = 2;
    public static final int YOU_SHARE = 3;
    public static final int YOU_CALL_CENTER = 4;
    public static final int YOUE_COUPON = 6;                  //优惠券
    public static final int WX_SHARE = 7;                     //分享
    public static final int SETTING = 5;
    private ImageView ivUserphoto;
    private TextView tvUserPhoneNum;
    private TextView tvCredit;
    private RecyclerView recyclerView;
    private UserInfoLogoAdapter userInfoAdapter;
    private static final int[] titles = {R.string.your_wallet, R.string.your_journey, R.string.your_coupon, R.string.wx_share, R.string.call_center, R.string.setting};
    private static final int[] types = {YOU_WALLET, YOU_JOURNEY, YOUE_COUPON, WX_SHARE, YOU_CALL_CENTER, SETTING};
    private static final int[] logos = {R.mipmap.your_wallet1, R.mipmap.your_journey1, R.mipmap.your_coupon1, R.mipmap.wx_share1, R.mipmap.call_center1, R.mipmap.setting1};

    private List<UserInfoSetBean> userInfos;


    @Override
    public void initTitleBar() {

    }

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        ivUserphoto = get(R.id.iv_user_photo);
        tvUserPhoneNum = get(R.id.tv_user_info_phone);
        tvCredit = get(R.id.tv_user_info_credit);
        recyclerView = get(R.id.recycler_user_info);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        userInfoAdapter = new UserInfoLogoAdapter(recyclerView);
        recyclerView.setAdapter(userInfoAdapter);
        userInfos = new ArrayList<>();
        setData();

    }

    public void setData() {
        userInfos.clear();
        for (int i = 0; i < titles.length; i++) {
            UserInfoSetBean userInfoSetBean = new UserInfoSetBean(getActivity().getString(titles[i]), types[i]);
            userInfoSetBean.setLogoSource(logos[i]);
            userInfos.add(userInfoSetBean);
        }
        userInfoAdapter.setData(userInfos);
    }


    public UserInfoLogoAdapter getUserInfoAdapter() {
        return userInfoAdapter;
    }

    public void setUserInfoAdapter(UserInfoLogoAdapter userInfoAdapter) {
        this.userInfoAdapter = userInfoAdapter;
    }

    public void updateUserInfo(UserBean userBean) {
        ImageLoader.getInstance().displayImage(userBean.getPhoto(), ivUserphoto, ImageLoaderHelper.getInstance(this.getActivity()).getDisplayAddHeader());
        tvCredit.setText(getActivity().getString(R.string.credit_score) + userBean.getCredits() + getActivity().getString(R.string.scrore));
        String amount = userBean.getAvailableAmount() + getActivity().getString(R.string.CNY);
        userInfos.get(0).setDescribe(amount);
        userInfoAdapter.notifyItemChanged(0);
        String phone = userBean.getMobile();
        if (CommonUtils.isNotEmpty(phone) && phone.length() == 11) {
            tvUserPhoneNum.setText(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }

    }
}
